# encoding: utf-8
class Users::SessionsController < Devise::SessionsController
  prepend_before_filter :require_no_authentication, :only =>:create
  skip_before_filter :verify_authenticity_token
  before_filter :login_required, :only => :destroy
  after_filter :set_login_cookie, :only => :create
  def create
    if params["mobile"] == "1"
    #from app
    else
      login = params[:user][:login]
      u = User.where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
      if u.active? && u.enabled?
        super
      else
        redirect_to active_page_path
      end
    end
  end
  
  private

  def set_login_cookie
    if logged_in? and params[:user][:remember_me] == '1'
      current_user.remember_me
      cookies[:auth_token] = {
        :value => current_user.remember_token,
        :expires => current_user.remember_token_expires_at }
    end
  end

end
